<html>
<head><meta charset="utf-8"><title>Finding a Función given a CrateDefMap · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html">Finding a Función given a CrateDefMap</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="221363970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221363970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221363970">(Jan 01 2021 at 18:59)</a>:</h4>
<p>A related question I also had was given a CrateDefMap and a path to a function. How would I get the data and the body of that function?</p>
<p>An example I had in mind was:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>

<span class="w">      </span><span class="k">fn</span> <span class="nf">a</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">_j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"></span>
<span class="w">     </span><span class="p">}</span><span class="w"></span>

<span class="w">     </span><span class="k">fn</span> <span class="nf">b</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I have a crateDefMap structure and I know the path to <code>a</code> is <code>foo::a</code> . Is there a way to recover the data on <code>a</code> such as it's visibility and it's content such as the let statement?</p>



<a name="221364555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221364555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221364555">(Jan 01 2021 at 19:13)</a>:</h4>
<hr>
<p>Oh so this is related because I was following the logic of the DefCollector which calls the ModCollector.</p>
<p>The ModCollector finds a function here: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/nameres/collector.rs#L1042">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/nameres/collector.rs#L1042</a></p>
<p>and extracts a DefData object from the found function. I noticed, the DefData object has the function name.</p>
<p>This line is then called: <a href="https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/nameres/collector.rs#L1168">https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir_def/src/nameres/collector.rs#L1168</a></p>
<p>Where the FunctionID is added to the module ItemScope(L1169), then the visibility is resolved relative to the module the function was defined in(L1170-1174) then on L115-1179 we update the namespaces, but I'm not too sure what is exactly happening here with the GlobImports types and ItemScope types.</p>



<a name="221364608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221364608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221364608">(Jan 01 2021 at 19:14)</a>:</h4>
<p><code>CrateDefMap</code> has a <code>resolve_path</code> function that you could use for this, but it's unlikely to be the right thing to use</p>



<a name="221364763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221364763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221364763">(Jan 01 2021 at 19:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap/near/221364608">said</a>:</p>
<blockquote>
<p><code>CrateDefMap</code> has a <code>resolve_path</code> function that you could use for this, but it's unlikely to be the right thing to use</p>
</blockquote>
<p>Is it because we no longer will need the function names once the DefMap is constructed?</p>



<a name="221364887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221364887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221364887">(Jan 01 2021 at 19:22)</a>:</h4>
<p>it just kind of depends on where that path actually comes from</p>



<a name="221364889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221364889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221364889">(Jan 01 2021 at 19:22)</a>:</h4>
<p><span class="user-mention silent" data-user-id="369769">kev</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap/near/221364763">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap/near/221364608">said</a>:</p>
<blockquote>
<p><code>CrateDefMap</code> has a <code>resolve_path</code> function that you could use for this, but it's unlikely to be the right thing to use</p>
</blockquote>
<p>Is it because we no longer will need the function names once the DefMap is constructed?</p>
</blockquote>
<p>For example, given:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">hello</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="n">world</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">world</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<p>Since <code>hello</code> calls <code>world</code> when I go to resolve <code>hello</code> I would need some way to access <code>world</code> wherever it is. I thought that this would be via it's name unless we somehow know the FunctionID for <code>world</code> when we are resolving <code>hello</code></p>



<a name="221365026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221365026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221365026">(Jan 01 2021 at 19:27)</a>:</h4>
<p>resolving <code>world</code> in that situation happens through a <code>Resolver</code>, which uses the <code>CrateDefMap</code> but also handles stuff like generics, local bindings etc.</p>



<a name="221365307"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221365307" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221365307">(Jan 01 2021 at 19:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap/near/221365026">said</a>:</p>
<blockquote>
<p>resolving <code>world</code> in that situation happens through a <code>Resolver</code>, which uses the <code>CrateDefMap</code> but also handles stuff like generics, local bindings etc.</p>
</blockquote>
<p>Oh I see, I guess since they are both from the same module, this can be resolved quite easily using the ItemScope which has a HashMap from Name to ModuleDefID.</p>
<p>If they are not in the same module, then I guess since there is also a HashMap linking ModuleNames to ModuleIDs then that case is also fine.</p>
<p>If they are not in the same Crate, then I guess you can look into the <code>extern_prelude</code> which has the CrateName and the ModuleDefId which would the FunctionID. From the CrateName you can get the CrateID and subsequently the CrateMap for that Crate.</p>
<p>Is this correct?</p>



<a name="221365520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221365520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221365520">(Jan 01 2021 at 19:40)</a>:</h4>
<p>that's roughly what's happening. See the <code>resolve_path_fp_with_macro</code> function, which does the actual work of resolving paths using the CrateDefMap</p>



<a name="221365828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Finding%20a%20Funci%C3%B3n%20given%20a%20CrateDefMap/near/221365828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kev <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap.html#221365828">(Jan 01 2021 at 19:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="129457">Florian Diebold</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Finding.20a.20Funci.C3.B3n.20given.20a.20CrateDefMap/near/221365520">said</a>:</p>
<blockquote>
<p>that's roughly what's happening. See the <code>resolve_path_fp_with_macro</code> function, which does the actual work of resolving paths using the CrateDefMap</p>
</blockquote>
<p>Ah okay thanks for the help, still debugging <code>resolve_path_fp_with_macro</code> with a test case to see how it works, I think my initial confusion has been cleared. Thank you</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>